//
//  FQDInfoWebViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/4/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDInfoWebViewController.h"
#import "FQDDotLoadingView.h"

@interface FQDInfoWebViewController () <UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *infoWebView;
// Loaidng界面
@property (nonatomic, strong) FQDDotLoadingView *loadingView;

@end

@implementation FQDInfoWebViewController

- (instancetype)initWithUrlString:(NSString *)urlString
{
    self = [super init];
    if (self) {
        _urlString = urlString;
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupNavigationBar];
    [self setupInfoWebView];
    [self startLoading];
}

- (void)setupNavigationBar
{
    NSDictionary *textAttributes = @{
        NSForegroundColorAttributeName : [UIColor whiteColor],
        NSFontAttributeName : FQDRegularFontWithSize(17.0f),
    };
    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:@"关闭"
                                    style:UIBarButtonItemStylePlain
                                    target:self
                                    action:@selector(close:)];
    [closeButton setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = closeButton;
    self.navigationController.navigationBar.titleTextAttributes = textAttributes;
}

- (void)setupInfoWebView
{
    self.infoWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    self.infoWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.infoWebView.delegate = self;
    self.infoWebView.backgroundColor = FQDInfoWebViewBackgroundColor;
    [self.view addSubview:self.infoWebView];
    
    [self loadUrlString:self.urlString];
    
}

- (void)startLoading
{
    self.loadingView = [[FQDDotLoadingView alloc] initWithFrame:self.view.bounds];
    [self.loadingView startLoading];
    [self.view addSubview:self.loadingView];
    
    self.infoWebView.alpha = 0;
}

- (void)endLoading
{
    [UIView animateWithDuration:0.3f animations:^{
        self.loadingView.alpha = 0;
        
        self.infoWebView.alpha = 1.0f;
    } completion:^(BOOL finished) {
        [self.loadingView removeFromSuperview];
    }];
}

- (void)loadUrlString:(NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.infoWebView loadRequest:request];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

#pragma mark - Event response
- (void)close:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self endLoading];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    FQDLog(@"InfoWebView fail to load:%@", [error localizedDescription]);
}

#pragma mark - Getters and Setters
- (void)setUrlString:(NSString *)urlString
{
    _urlString = urlString;
    
    [self loadUrlString:urlString];
}

@end
